package ptij.vocoder.baseModel.lp_analysis;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.BooleanToken;
import ptolemy.data.IntToken;
import ptolemy.data.type.*;
import ptolemy.data.expr.Parameter;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// 
/** 

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class VAD_Lp extends TypedAtomicActor {

    public VAD_Lp(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);
        
        tx_dtx_in = new TypedIOPort(this, "tx_dtx_in", true, false);
        tx_dtx_in.setTypeEquals(BaseType.OBJECT);

        r_h = new TypedIOPort(this, "r_h", true, false);
        r_h.setTypeEquals(BaseType.OBJECT);
        
        r_l = new TypedIOPort(this, "r_l", true, false);
        r_l.setTypeEquals(BaseType.OBJECT);
        
        scal_acf = new TypedIOPort(this, "scal_acf", true, false);
        scal_acf.setTypeEquals(BaseType.OBJECT);
        
        rc = new TypedIOPort(this, "rc", true, false);
        rc.setTypeEquals(BaseType.OBJECT);
        
        ptch = new TypedIOPort(this, "ptch", true, false);
        ptch.setTypeEquals(BaseType.BOOLEAN);
        
        reset = new TypedIOPort(this, "reset", true, false);
        reset.setTypeEquals(BaseType.BOOLEAN);
        
        txdtx_ctrl = new TypedIOPort(this, "txdtx_ctrl", false, true);
        txdtx_ctrl.setTypeEquals(BaseType.OBJECT);
        
        vad = new TypedIOPort(this, "vad", false, true);
        vad.setTypeEquals(BaseType.BOOLEAN);
        
        L_pn_seed_tx = new TypedIOPort(this, "L_pn_seed_tx", false, true);
        L_pn_seed_tx.setTypeEquals(BaseType.INT);
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports    
    public TypedIOPort tx_dtx_in;
    public TypedIOPort r_h;
    public TypedIOPort r_l;
    public TypedIOPort scal_acf;
    public TypedIOPort rc;
    public TypedIOPort ptch;
    public TypedIOPort reset;

    // Output Ports
    public TypedIOPort txdtx_ctrl;
    public TypedIOPort vad;
    public TypedIOPort L_pn_seed_tx;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
        _reset();
        
        // dtxmode parametro!!!!!
        
    }
    
    public void fire() throws IllegalActionException {
        if(reset.hasToken(0)) {
            reset.get(0);
            _reset();
        }

        if(_dtx_mode) {
            ObjectToken obj = (ObjectToken) r_h.get(0);
            Short [] arg1 = (Short []) obj.getValue();
            
            obj = (ObjectToken) r_l.get(0);
            Short [] arg2 = (Short []) obj.getValue();
            
            obj = (ObjectToken) scal_acf.get(0);
            Short arg3 = (Short) obj.getValue();
            
            obj = (ObjectToken) rc.get(0);
            Short [] arg4 = (Short []) obj.getValue();
            
            BooleanToken bt = (BooleanToken) ptch.get(0);
            boolean arg5 = bt.booleanValue();
            
            boolean VAD_flag = _vad_computation(arg1, arg2, arg3, arg4, arg5);

            obj = (ObjectToken) tx_dtx_in.get(0);
            Short txdtx = (Short) obj.getValue();

            Integer l_pn_seed = new Integer(0);
            _tx_dtx(VAD_flag, txdtx, l_pn_seed);
            
            txdtx_ctrl.broadcast(new ObjectToken(txdtx));
            L_pn_seed_tx.broadcast(new IntToken(l_pn_seed.intValue()));
        }
        else {
            // VAD_Flag ???

            txdtx_ctrl.broadcast(new ObjectToken(
                new Short((short) (Cnst.TX_VAD_FLAG | Cnst.TX_SP_FLAG))));
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////
 
    private void _reset() throws IllegalActionException  {
        Parameter par = (Parameter) getAttribute("dtx_mode");
        if(par != null) {
            BooleanToken it = (BooleanToken) par.getToken();
            _dtx_mode = it.booleanValue();
        }
        
    }

    private boolean _vad_computation(Short [] r_h, Short [] r_l, Short scal_acf,
            Short [] rc, boolean ptch) {

        return false;
    }

    private void _tx_dtx(boolean VAD_flag, Short txdtx_ctrl, Integer l_pn_seed) {
        
        
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private boolean _dtx_mode;
    
}
